//Bad Block
//Matt Cramer, Jesse Martinez, Mark Mendoza

#include "../QueueManager/QueueManager.h"
#include "../FileSystem/FileSystem.h"
int incrementBU = 11;
file_control_block *Create_Bad_Block(file_control_block* queue, int Bad)
{
	if(queue[Bad].badBlock == 0)
	{
		queue[10].data = -1;
	}
	else if(Bad == 266)
	{
		queue[10].data = -2;
	}
	else if(Bad == 267)
	{
		queue[10].data = -3;
	}
	else
	{
		queue[Bad].badBlock = 0;
	}
	return queue;
}

file_control_block* isBad(file_control_block* queue, int Bad, int size)
{
	if(queue[Bad].badBlock == -1)
	{
		queue[10].data = 0; //0 is No Bad Block
	}
	else
	{
		if(queue[Bad].frameType == -1 && incrementBU <=265)
		{
			queue[10].data = incrementBU;
			incrementBU++;
		}
		else if(queue[Bad].frameType == -1 && incrementBU >265)
		{
			queue[10].data = 1;
			
		}
		else if(queue[Bad].frameType == 1)
		{
			int temp =0;
			int z=0;
			while(temp == 0)
			{
				if(queue[267].pointers[z].location == Bad)
				{
					temp = 1;
				}
				else
				{
					z++;
				}
			}
			create_directory(queue[267].pointers[z].name);
			temp =0;
			int l = 0;
			while(temp == 0)
			{
				if(queue[267].pointers[l].name == queue[267].pointers[z].name)
				{
					temp =1;
				}
				else
				{
					l++;
				}
			}
			
			int e = z;
			while(e<queue[267].ptrSize-1)
			{
				queue[e] = queue[e+1];
				e++;
			}
			
			int k=268;
			while(k<size)
			{
				if(queue[k].parentPtr == Bad)
				{
					queue[k].parentPtr = queue[266].pointers[l].location;
				}
				k++;
			}
		}
		else if(queue[Bad].frameType == 2)
		{
			//createfilehead(queue[267].pointer[Bad].name);
			int k=268;
			while(k<size)
			{
				if(queue[k].parentPtr == Bad)
				{
					queue[k].parentPtr = queue[267].pointers[Bad].location;
				}
				k++;
			}
		}
		else if(queue[Bad].frameType == 3)
		{
			int i, k, j, dirLoc, fileLoc;
			for(i=0; i<queue[267].ptrSize; i++)
			{
				for(k=0; k<queue[queue[267].pointers[i].location].ptrSize; k++)
				{
					dirLoc = queue[267].pointers[i].location;
					fileLoc = queue[dirLoc].pointers[k].location;
					for(j=0; j<queue[fileLoc].ptrSize; j++)
					{
						if(queue[fileLoc].pointers[j].location == Bad)
						{
							int temp[60];
							temp[60] = deleteObjects(queue[dirLoc].ID, queue[fileLoc].ID);
							if(temp[0] == -2)
							{
								closeFile(queue[dirLoc].ID, queue[fileLoc].ID);
								deleteObjects(queue[dirLoc].ID, queue[fileLoc].ID);
							}
							else if(temp[0] >=0)
							{
							}
							else
								queue[10].data = -4;
								
						}
					}
				}
			}
				queue[10].data = 5;
				
		}
	}	
	return queue;
}
